package com.shenhaofeng.app.loader.content.component;

import android.os.Bundle;
import android.os.Looper;
import android.support.v4.widget.ContentLoadingProgressBar;
import android.view.View;

import com.shenhaofeng.app.loader.content.Component;
import com.shenhaofeng.app.loader.content.ContentSource;
import com.shenhaofeng.app.loader.content.OnBindContentListener;

import java.util.concurrent.Executor;

import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;


/**
 * 内容加载实现,听过ContentLoadingProgress展示加载状态
 * Created by Administrator on 2016/9/7.
 */
public class ContentLoadingComponent<R> extends Component<R> {


    private OnContentLoadingErrorListener mOnContentLoadingErrorListener;

    public ContentLoadingComponent() {
    }

    public ContentLoadingComponent(OnContentLoadingErrorListener mOnContentLoadingErrorListener) {
        this.mOnContentLoadingErrorListener = mOnContentLoadingErrorListener;
    }

    public ContentLoadingComponent(Executor mExecutor, OnContentLoadingErrorListener mOnContentLoadingErrorListener) {
        super(mExecutor);
        this.mOnContentLoadingErrorListener = mOnContentLoadingErrorListener;
    }

    public ContentLoadingComponent(Looper mLooper, OnContentLoadingErrorListener mOnContentLoadingErrorListener) {
        super(mLooper);
        this.mOnContentLoadingErrorListener = mOnContentLoadingErrorListener;
    }

    @Override
    protected void onPreLoad(final View statusView, final View contentView) {
        final ContentLoadingProgressBar contentLoadingProgressBar = (ContentLoadingProgressBar) statusView;
        contentLoadingProgressBar.post(new Runnable() {
            @Override
            public void run() {
                contentLoadingProgressBar.show();
                contentView.setVisibility(hasHideContentView() ? View.VISIBLE : View.GONE);
            }
        });
    }

    @Override
    protected void onLoad(final View statusView, final View contentView, Bundle args, final OnBindContentListener<R> listener, ContentSource<R> contentSource) {
        final ContentLoadingProgressBar contentLoadingProgressBar = (ContentLoadingProgressBar) statusView;
        contentSource.onRequestContent(args)
                .subscribeOn(getWorkScheduler())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<R>() {
                    @Override
                    public void call(final R r) {
                        contentView.post(new Runnable() {
                            @Override
                            public void run() {
                                contentLoadingProgressBar.hide();
                                contentView.setVisibility(View.VISIBLE);
                                listener.onBindContent(contentView, r);
                            }
                        });

                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(final Throwable throwable) {
                        contentLoadingProgressBar.post(new Runnable() {
                            @Override
                            public void run() {
                                contentLoadingProgressBar.hide();
                                if (mOnContentLoadingErrorListener != null) {
                                    mOnContentLoadingErrorListener.onError(throwable);
                                }
                            }
                        });

                    }
                });
    }

    @Override
    protected void onInterruptLoad(View statusView, final View contentView) {
        final ContentLoadingProgressBar contentLoadingProgressBar = (ContentLoadingProgressBar) statusView;
        contentLoadingProgressBar.post(new Runnable() {
            @Override
            public void run() {
                contentLoadingProgressBar.show();
                contentView.setVisibility(hasHideContentView() ? View.VISIBLE : View.GONE);
            }
        });
    }

    @Override
    protected boolean hasContentView(View statusView, View contentView) {
        return statusView instanceof ContentLoadingProgressBar;
    }


    /**
     * 在显示加载状态时是否隐藏Content的界面
     *
     * @return
     */
    protected boolean hasHideContentView() {
        return false;
    }

    public interface OnContentLoadingErrorListener {
        void onError(Throwable throwable);
    }


}
